﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace hyjiacan.util.web.wp
{
    public partial class SettingsForm : hyjiacan.util.winform.JForm
    {
        public SettingsForm(string title)
        {
            InitializeComponent();

            CheckForIllegalCrossThreadCalls = false;

            this.title.Text = title;
            configLoader.RunWorkerAsync("get");
        }
        /// <summary>
        /// 加载或保存设置项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void configLoader_DoWork(object sender, DoWorkEventArgs e)
        {
            string arg = e.Argument.ToString();

            if (arg == "get")
            {
                SetStatus("正在加载配置项...");
                // 读配置
                this.tServer.Text = Settings.Get(Settings.Items.Server);
                this.tUsername.Text = Settings.Get(Settings.Items.Username);
                this.tPassword.Text = Settings.Get(Settings.Items.Password);
                this.tDatabase.Text = Settings.Get(Settings.Items.Database);
                this.tPrefix.Text = Settings.Get(Settings.Items.Prefix);
                SetStatus("加载配置项完成");
            }
            else if (arg == "set")
            {
                SetStatus("正在保存配置项...");
                //写配置
                Settings.Set(Settings.Items.Server, tServer.Text);
                Settings.Set(Settings.Items.Username, tUsername.Text);
                Settings.Set(Settings.Items.Password, tPassword.Text);
                Settings.Set(Settings.Items.Database, tDatabase.Text);
                Settings.Set(Settings.Items.Prefix, tPrefix.Text);
                SetStatus("保存配置项完成");
            }
            else if (arg == "test")
            {
                SetStatus("正在测试配置项...");
                // 测试配置
                try
                {

                    DBUtil.Connect(tServer.Text, tUsername.Text, tPassword.Text, tDatabase.Text).Close();
                    SetStatus("测试配置项成功", Color.Green);
                }
                catch (Exception ex)
                {
                    SetStatus("测试配置项失败", Color.Red);
                    MessageBox.Show(this, ex.Message, "测试连接失败");
                }

            }
        }

        private void bTest_Click(object sender, EventArgs e)
        {
            configLoader.RunWorkerAsync("test");
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            configLoader.RunWorkerAsync("set");
        }

        private void SetStatus(string msg)
        {
            SetStatus(msg, Color.Black);
        }

        private void SetStatus(string msg, Color color)
        {
            this.status.ForeColor = color;
            this.status.Text = msg;
        }
    }
}
